var HxBasicGoodsInfoDlg = {
    data: {
        id: "",
        goodsName: "",
        goodsNumber: "",
    }
};
var BasicPutgoodsInfoDlg = {
    data: {
        id: "",
        dictId: "",
        putgoodsCustomerId: "",
        putgoodsOperateName: "",
        putgoodsName: "",
    }
};
var DictInfoDlg = {
    data: {
        dictId: "",
        name: ""
    }
};
layui.use(['table', 'admin', 'ax', 'laydate', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var form = layui.form;

    laydate.render({
        elem: '#startTime'
        , value: new Date()
    });
    laydate.render({
        elem: '#endTime'
        , value: new Date()
    });

    var tableResult

    /**
     * 库存表管理
     */
    var HxStorageStock = {
        tableId: "hxStorageStockTable"
    };

    var sl = {
        baseNum: "",
        kcNum: "",
        cgNum: "",
        pfNum: "",
        psNum: "",
        stockWeight: "",
        stockVolume: "",
        stockMoney: ""
    }
    
    // 清空库存
    $('#clearAll').click(function () {
		var warehouseId = $("#warehouseId").val();
		if (warehouseId==null||warehouseId=='') {
			layer.msg('请选择仓库');
		} else {
	        var operation = function () {
	            var ajax = new $ax(Feng.ctxPath + "/hxStorageStock/clearAll?warehouseId="+warehouseId, function (data) {
	                if(data.success){
	                	Feng.success("清空成功!");
                		table.reload(HxStorageStock.tableId);
	                } else {
	                    Feng.error("清空失败!" + data.message + "!");
	                }
	            }, function (data) {
	                Feng.error("清空失败!" + data.responseJSON.message + "!");
	            });
	            ajax.start();
	        };
	        Feng.confirm("确定清空库存?", operation);
		}
    });

    /**
     * 初始化表格的列kc   mx
     */
    HxStorageStock.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'stockState', sort: true, title: '状态', templet: '#stockStateTpl'},
            {field: 'operateName', sort: true, title: '项目', hide: true},
            {field: 'customerName', sort: true, title: '客户', hide: true},

            {field: 'warehouseName', sort: true, title: '仓库'},
            {field: 'areaName', sort: true, title: '库区名称'},
            {field: 'areaNumber', sort: true, title: '库区'},
            {field: 'positionNumber', sort: true, title: '库位'},

            {field: 'goodsName', sort: true, title: '商品名称', width: 160},
            {field: 'goodsNumber', sort: true, title: '商品'},
            {field: 'goodsPcode69', sort: true, title: '商品码'},
            {field: 'goodsSpecs', sort: true, title: '规格'},
            {field: 'goodsBigtype', sort: true, title: '分类'},

            {field: 'kcUnit', sort: true, title: '库存单位', totalRowText: '合计：'},
            {field: 'kcNum', sort: true, title: '库存数量', totalRowText: sl.kcNum},
            {field: 'jibenUnit', sort: true, title: '基本单位'},
            {field: 'baseNum', sort: true, title: '基本数量', totalRowText: sl.baseNum},

            {field: 'stockWeight', sort: true, title: '重量', totalRowText: sl.stockWeight, hide: true},
            {field: 'stockVolume', sort: true, title: '体积', totalRowText: sl.stockVolume, hide: true},
            {field: 'goodsPrice', sort: true, title: '单价', hide: true},
            {field: 'stockMoney', sort: true, title: '金额', totalRowText: sl.stockMoney, hide: true},

            {field: 'detailedDate', sort: true, title: '生产日期'},
            {field: 'goodsWarranty', sort: true, title: '保质期'},
            {field: 'setUnit', sort: true, title: '发货客户'},

        ]];
    };
    /**
     * 初始化表格的列cg  mx
     */
    HxStorageStock.initColumn1 = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'stockState', sort: true, title: '状态', templet: '#stockStateTpl'},
            {field: 'operateName', sort: true, title: '项目', hide: true},
            {field: 'customerName', sort: true, title: '客户', hide: true},

            {field: 'warehouseName', sort: true, title: '仓库'},
            {field: 'areaName', sort: true, title: '库区名称'},
            {field: 'areaNumber', sort: true, title: '库区'},
            {field: 'positionNumber', sort: true, title: '库位'},

            {field: 'goodsName', sort: true, title: '商品名称', width: 160},
            {field: 'goodsNumber', sort: true, title: '商品'},
            {field: 'goodsPcode69', sort: true, title: '商品码'},
            {field: 'goodsSpecs', sort: true, title: '规格'},
            {field: 'goodsBigtype', sort: true, title: '分类'},

            {field: 'cgUnit', sort: true, title: '采购单位', totalRowText: '合计：'},
            {field: 'cgNum', sort: true, title: '采购数量', totalRowText: sl.cgNum},
            {field: 'jibenUnit', sort: true, title: '基本单位'},
            {field: 'baseNum', sort: true, title: '基本数量', totalRowText: sl.baseNum},

            {field: 'stockWeight', sort: true, title: '重量', totalRowText: sl.stockWeight},
            {field: 'stockVolume', sort: true, title: '体积', totalRowText: sl.stockVolume},
            {field: 'goodsPrice', sort: true, title: '单价'},
            {field: 'stockMoney', sort: true, title: '金额', totalRowText: sl.stockMoney},

            {field: 'detailedDate', sort: true, title: '生产日期'},
            {field: 'goodsWarranty', sort: true, title: '保质期'},
            {field: 'setUnit', sort: true, title: '发货客户'},
        ]];
    };
    /**
     * 初始化表格的列pf   mx
     */
    HxStorageStock.initColumn2 = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'stockState', sort: true, title: '状态', templet: '#stockStateTpl'},
            {field: 'operateName', sort: true, title: '项目', hide: true},
            {field: 'customerName', sort: true, title: '客户', hide: true},

            {field: 'warehouseName', sort: true, title: '仓库'},
            {field: 'areaName', sort: true, title: '库区名称'},
            {field: 'areaNumber', sort: true, title: '库区'},
            {field: 'positionNumber', sort: true, title: '库位'},

            {field: 'goodsName', sort: true, title: '商品名称', width: 160},
            {field: 'goodsNumber', sort: true, title: '商品'},
            {field: 'goodsPcode69', sort: true, title: '商品码'},
            {field: 'goodsSpecs', sort: true, title: '规格'},
            {field: 'goodsBigtype', sort: true, title: '分类'},

            {field: 'pfUnit', sort: true, title: '批发单位', totalRowText: '合计：'},
            {field: 'pfNum', sort: true, title: '批发数量', totalRowText: sl.pfNum},
            {field: 'jibenUnit', sort: true, title: '基本单位'},
            {field: 'baseNum', sort: true, title: '基本数量', totalRowText: sl.baseNum},

            {field: 'stockWeight', sort: true, title: '重量', totalRowText: sl.stockWeight},
            {field: 'stockVolume', sort: true, title: '体积', totalRowText: sl.stockVolume},
            {field: 'goodsPrice', sort: true, title: '单价'},
            {field: 'stockMoney', sort: true, title: '金额', totalRowText: sl.stockMoney},

            {field: 'detailedDate', sort: true, title: '生产日期'},
            {field: 'goodsWarranty', sort: true, title: '保质期'},
            {field: 'setUnit', sort: true, title: '发货客户'},
        ]];
    };
    /**
     * 初始化表格的列ps   mx
     */
    HxStorageStock.initColumn3 = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'stockState', sort: true, title: '状态', templet: '#stockStateTpl'},
            {field: 'operateName', sort: true, title: '项目', hide: true},
            {field: 'customerName', sort: true, title: '客户', hide: true},

            {field: 'warehouseName', sort: true, title: '仓库'},
            {field: 'areaName', sort: true, title: '库区名称'},
            {field: 'areaNumber', sort: true, title: '库区'},
            {field: 'positionNumber', sort: true, title: '库位'},

            {field: 'goodsName', sort: true, title: '商品名称', width: 160},
            {field: 'goodsNumber', sort: true, title: '商品'},
            {field: 'goodsPcode69', sort: true, title: '商品码'},
            {field: 'goodsSpecs', sort: true, title: '规格'},
            {field: 'goodsBigtype', sort: true, title: '分类'},

            {field: 'psUnit', sort: true, title: '配送单位', totalRowText: '合计：'},
            {field: 'psNum', sort: true, title: '配送数量', totalRowText: sl.psNum},
            {field: 'jibenUnit', sort: true, title: '基本单位'},
            {field: 'baseNum', sort: true, title: '基本数量', totalRowText: sl.baseNum},

            {field: 'stockWeight', sort: true, title: '重量', totalRowText: sl.stockWeight},
            {field: 'stockVolume', sort: true, title: '体积', totalRowText: sl.stockVolume},
            {field: 'goodsPrice', sort: true, title: '单价'},
            {field: 'stockMoney', sort: true, title: '金额', totalRowText: sl.stockMoney},

            {field: 'detailedDate', sort: true, title: '生产日期'},
            {field: 'goodsWarranty', sort: true, title: '保质期'},
            {field: 'setUnit', sort: true, title: '发货客户'},
        ]];
    };
    /**
     * 初始化表格的列kc  hz
     */
    HxStorageStock.initColumn4 = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'stockState', sort: true, title: '状态', templet: '#stockStateTpl'},
            {field: 'operateName', sort: true, title: '项目', hide: true},
            {field: 'customerName', sort: true, title: '客户', hide: true},
            {field: 'warehouseName', sort: true, title: '仓库'},

            {field: 'goodsName', sort: true, title: '商品名称', width: 160},
            {field: 'goodsNumber', sort: true, title: '商品'},
            {field: 'goodsPcode69', sort: true, title: '商品码'},
            {field: 'goodsSpecs', sort: true, title: '规格'},
            {field: 'goodsBigtype', sort: true, title: '分类'},

            {field: 'kcUnit', sort: true, title: '库存单位', totalRowText: '合计：'},
            {field: 'kcNum', sort: true, title: '库存数量', totalRowText: sl.kcNum},
            {field: 'jibenUnit', sort: true, title: '基本单位'},
            {field: 'baseNum', sort: true, title: '基本数量', totalRowText: sl.baseNum},

            {field: 'stockWeight', sort: true, title: '重量', totalRowText: sl.stockWeight},
            {field: 'stockVolume', sort: true, title: '体积', totalRowText: sl.stockVolume},
            {field: 'goodsPrice', sort: true, title: '单价'},
            {field: 'stockMoney', sort: true, title: '金额', totalRowText: sl.stockMoney},

            {field: 'setUnit', sort: true, title: '发货客户'},
        ]];
    };
    /**
     * 初始化表格的列cg   hz
     */
    HxStorageStock.initColumn5 = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'stockState', sort: true, title: '状态', templet: '#stockStateTpl'},
            {field: 'operateName', sort: true, title: '项目', hide: true},
            {field: 'customerName', sort: true, title: '客户', hide: true},
            {field: 'warehouseName', sort: true, title: '仓库'},

            {field: 'goodsName', sort: true, title: '商品名称', width: 160},
            {field: 'goodsNumber', sort: true, title: '商品'},
            {field: 'goodsPcode69', sort: true, title: '商品码'},
            {field: 'goodsSpecs', sort: true, title: '规格'},
            {field: 'goodsBigtype', sort: true, title: '分类'},

            {field: 'cgUnit', sort: true, title: '采购单位', totalRowText: '合计：'},
            {field: 'cgNum', sort: true, title: '采购数量', totalRowText: sl.cgNum},
            {field: 'jibenUnit', sort: true, title: '基本单位'},
            {field: 'baseNum', sort: true, title: '基本数量', totalRowText: sl.baseNum},

            {field: 'stockWeight', sort: true, title: '重量', totalRowText: sl.stockWeight},
            {field: 'stockVolume', sort: true, title: '体积', totalRowText: sl.stockVolume},
            {field: 'goodsPrice', sort: true, title: '单价'},
            {field: 'stockMoney', sort: true, title: '金额', totalRowText: sl.stockMoney},

            {field: 'setUnit', sort: true, title: '发货客户'},
        ]];
    };
    /**
     * 初始化表格的列pf  hz
     */
    HxStorageStock.initColumn6 = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'stockState', sort: true, title: '状态', templet: '#stockStateTpl'},
            {field: 'operateName', sort: true, title: '项目', hide: true},
            {field: 'customerName', sort: true, title: '客户', hide: true},
            {field: 'warehouseName', sort: true, title: '仓库'},

            {field: 'goodsName', sort: true, title: '商品名称', width: 160},
            {field: 'goodsNumber', sort: true, title: '商品'},
            {field: 'goodsPcode69', sort: true, title: '商品码'},
            {field: 'goodsSpecs', sort: true, title: '规格'},
            {field: 'goodsBigtype', sort: true, title: '分类'},
            {field: 'pfUnit', sort: true, title: '批发单位', totalRowText: '合计：'},
            {field: 'pfNum', sort: true, title: '批发数量', totalRowText: sl.pfNum},
            {field: 'jibenUnit', sort: true, title: '基本单位'},
            {field: 'baseNum', sort: true, title: '基本数量', totalRowText: sl.baseNum},

            {field: 'stockWeight', sort: true, title: '重量', totalRowText: sl.stockWeight},
            {field: 'stockVolume', sort: true, title: '体积', totalRowText: sl.stockVolume},
            {field: 'goodsPrice', sort: true, title: '单价'},
            {field: 'stockMoney', sort: true, title: '金额', totalRowText: sl.stockMoney},

            {field: 'setUnit', sort: true, title: '发货客户'},
        ]];
    };
    /**
     * 初始化表格的列ps  hz
     */
    HxStorageStock.initColumn7 = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'stockState', sort: true, title: '状态', templet: '#stockStateTpl'},
            {field: 'operateName', sort: true, title: '项目', hide: true},
            {field: 'customerName', sort: true, title: '客户', hide: true},
            {field: 'warehouseName', sort: true, title: '仓库'},

            {field: 'goodsName', sort: true, title: '商品名称', width: 160},
            {field: 'goodsNumber', sort: true, title: '商品'},
            {field: 'goodsPcode69', sort: true, title: '商品码'},
            {field: 'goodsSpecs', sort: true, title: '规格'},
            {field: 'goodsBigtype', sort: true, title: '分类'},

            {field: 'psUnit', sort: true, title: '配送单位', totalRowText: '合计：'},
            {field: 'psNum', sort: true, title: '配送数量', totalRowText: sl.psNum},
            {field: 'jibenUnit', sort: true, title: '基本单位'},
            {field: 'baseNum', sort: true, title: '基本数量', totalRowText: sl.baseNum},

            {field: 'stockWeight', sort: true, title: '重量', totalRowText: sl.stockWeight},
            {field: 'stockVolume', sort: true, title: '体积', totalRowText: sl.stockVolume},
            {field: 'goodsPrice', sort: true, title: '单价'},
            {field: 'stockMoney', sort: true, title: '金额', totalRowText: sl.stockMoney},

            {field: 'setUnit', sort: true, title: '发货客户'},
        ]];
    };
    /**
     * 初始化表格的列kc   kw
     */
    HxStorageStock.initColumn8 = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'stockState', sort: true, title: '状态', templet: '#stockStateTpl'},
            {field: 'operateName', sort: true, title: '项目', hide: true},
            {field: 'customerName', sort: true, title: '客户', hide: true},

            {field: 'warehouseName', sort: true, title: '仓库'},
            {field: 'areaName', sort: true, title: '库区名称'},
            {field: 'areaNumber', sort: true, title: '库区'},
            {field: 'positionNumber', sort: true, title: '库位'},

            {field: 'goodsName', sort: true, title: '商品名称', width: 160},
            {field: 'goodsNumber', sort: true, title: '商品'},
            {field: 'goodsPcode69', sort: true, title: '商品码'},
            {field: 'goodsSpecs', sort: true, title: '规格'},
            {field: 'goodsBigtype', sort: true, title: '分类'},

            {field: 'kcUnit', sort: true, title: '库存单位', totalRowText: '合计：'},
            {field: 'kcNum', sort: true, title: '库存数量', totalRowText: sl.kcNum},
            {field: 'jibenUnit', sort: true, title: '基本单位'},
            {field: 'baseNum', sort: true, title: '基本数量', totalRowText: sl.baseNum},

            {field: 'stockWeight', sort: true, title: '重量', totalRowText: sl.stockWeight},
            {field: 'stockVolume', sort: true, title: '体积', totalRowText: sl.stockVolume},
            {field: 'goodsPrice', sort: true, title: '单价'},
            {field: 'stockMoney', sort: true, title: '金额', totalRowText: sl.stockMoney},

            {field: 'setUnit', sort: true, title: '发货客户'},

        ]];
    };
    /**
     * 初始化表格的列cg  kw
     */
    HxStorageStock.initColumn9 = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'stockState', sort: true, title: '状态', templet: '#stockStateTpl'},
            {field: 'operateName', sort: true, title: '项目', hide: true},
            {field: 'customerName', sort: true, title: '客户', hide: true},

            {field: 'warehouseName', sort: true, title: '仓库'},
            {field: 'areaName', sort: true, title: '库区名称'},
            {field: 'areaNumber', sort: true, title: '库区'},
            {field: 'positionNumber', sort: true, title: '库位'},

            {field: 'goodsName', sort: true, title: '商品名称', width: 160},
            {field: 'goodsNumber', sort: true, title: '商品'},
            {field: 'goodsPcode69', sort: true, title: '商品码'},
            {field: 'goodsSpecs', sort: true, title: '规格'},
            {field: 'goodsBigtype', sort: true, title: '分类'},

            {field: 'cgUnit', sort: true, title: '采购单位', totalRowText: '合计：'},
            {field: 'cgNum', sort: true, title: '采购数量', totalRowText: sl.cgNum},
            {field: 'jibenUnit', sort: true, title: '基本单位'},
            {field: 'baseNum', sort: true, title: '基本数量', totalRowText: sl.baseNum},

            {field: 'stockWeight', sort: true, title: '重量', totalRowText: sl.stockWeight},
            {field: 'stockVolume', sort: true, title: '体积', totalRowText: sl.stockVolume},
            {field: 'goodsPrice', sort: true, title: '单价'},
            {field: 'stockMoney', sort: true, title: '金额', totalRowText: sl.stockMoney},

            {field: 'setUnit', sort: true, title: '发货客户'},
        ]];
    };
    /**
     * 初始化表格的列pf   kw
     */
    HxStorageStock.initColumn10 = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'stockState', sort: true, title: '状态', templet: '#stockStateTpl'},
            {field: 'operateName', sort: true, title: '项目', hide: true},
            {field: 'customerName', sort: true, title: '客户', hide: true},

            {field: 'warehouseName', sort: true, title: '仓库'},
            {field: 'areaName', sort: true, title: '库区名称'},
            {field: 'areaNumber', sort: true, title: '库区'},
            {field: 'positionNumber', sort: true, title: '库位'},

            {field: 'goodsName', sort: true, title: '商品名称', width: 160},
            {field: 'goodsNumber', sort: true, title: '商品'},
            {field: 'goodsPcode69', sort: true, title: '商品码'},
            {field: 'goodsSpecs', sort: true, title: '规格'},
            {field: 'goodsBigtype', sort: true, title: '分类'},

            {field: 'pfUnit', sort: true, title: '批发单位', totalRowText: '合计：'},
            {field: 'pfNum', sort: true, title: '批发数量', totalRowText: sl.pfNum},
            {field: 'jibenUnit', sort: true, title: '基本单位'},
            {field: 'baseNum', sort: true, title: '基本数量', totalRowText: sl.baseNum},

            {field: 'stockWeight', sort: true, title: '重量', totalRowText: sl.stockWeight},
            {field: 'stockVolume', sort: true, title: '体积', totalRowText: sl.stockVolume},
            {field: 'goodsPrice', sort: true, title: '单价'},
            {field: 'stockMoney', sort: true, title: '金额', totalRowText: sl.stockMoney},

            {field: 'setUnit', sort: true, title: '发货客户'},
        ]];
    };
    /**
     * 初始化表格的列ps   kw
     */
    HxStorageStock.initColumn11 = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'stockState', sort: true, title: '状态', templet: '#stockStateTpl'},
            {field: 'operateName', sort: true, title: '项目', hide: true},
            {field: 'customerName', sort: true, title: '客户', hide: true},

            {field: 'warehouseName', sort: true, title: '仓库'},
            {field: 'areaName', sort: true, title: '库区名称'},
            {field: 'areaNumber', sort: true, title: '库区'},
            {field: 'positionNumber', sort: true, title: '库位'},

            {field: 'goodsName', sort: true, title: '商品名称', width: 160},
            {field: 'goodsNumber', sort: true, title: '商品'},
            {field: 'goodsPcode69', sort: true, title: '商品码'},
            {field: 'goodsSpecs', sort: true, title: '规格'},
            {field: 'goodsBigtype', sort: true, title: '分类'},
            {field: 'psUnit', sort: true, title: '配送单位', totalRowText: '合计：'},
            {field: 'psNum', sort: true, title: '配送数量', totalRowText: sl.psNum},
            {field: 'jibenUnit', sort: true, title: '基本单位'},
            {field: 'baseNum', sort: true, title: '基本数量', totalRowText: sl.baseNum},

            {field: 'stockWeight', sort: true, title: '重量', totalRowText: sl.stockWeight},
            {field: 'stockVolume', sort: true, title: '体积', totalRowText: sl.stockVolume},
            {field: 'goodsPrice', sort: true, title: '单价'},
            {field: 'stockMoney', sort: true, title: '金额', totalRowText: sl.stockMoney},

            {field: 'setUnit', sort: true, title: '发货客户'},
        ]];
    };
    HxStorageStock.kais = function () {
        $.ajax({
            url: Feng.ctxPath + '/hxStorageStock/list2?stockUnit=kc&queryMode=mx',
            dataType: "json",   //返回格式为json
            async: false,
            type: "post",   //请求方式
            success: function (data) {
                sl.kcNum = data.data.kcNum
                sl.baseNum = data.data.baseNum
                sl.stockWeight = data.data.stockWeight
                sl.stockVolume = data.data.stockVolume
                sl.stockMoney = data.data.stockMoney
            }
        });

    };

    HxStorageStock.kais();

    tableResult = table.render({
        elem: '#' + HxStorageStock.tableId,
        url: Feng.ctxPath + '/hxStorageStock/list',
        page: true,
        limit: 120,
        limits:[60,120,240,360,480,600,2000,5000],
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        totalRow: true,//开启合计行
        cols: HxStorageStock.initColumn()
    });

    /**
     * 点击查询按钮
     */
    HxStorageStock.search = function () {
        var queryData = {};
        queryData['stockState'] = $("#stockState").val();
        queryData['setUnit'] = $("#setUnit").val();
        queryData['goodsBigtypeId'] = $("#goodsBigtypeId").val();
        queryData['goodsName'] = $("#goodsName").val();
        queryData['customerId'] = $("#customerId").val();
        queryData['warehouseId'] = $("#warehouseId").val();
        queryData['areaId'] = $("#areaId").val();
        queryData['positionNumber'] = $("#positionNumber").val();
        queryData['positionType'] = $("#positionType").val();
        queryData['trayNumber'] = $("#trayNumber").val();
        queryData['condition'] = $("#condition").val();

        var stockUnit = $("#stockUnit").val();
        var queryMode = $("#queryMode").val();
        if (stockUnit == "kc" && queryMode == "mx") {
            $.ajax({
                url: Feng.ctxPath + '/hxStorageStock/list2?stockUnit=kc&queryMode=mx',
                dataType: "json",   //返回格式为json
                data: queryData, // 将 queryData 作为请求体发送
                async: false,
                type: "post",   //请求方式
                success: function (data) {
                    sl.kcNum = data.data.kcNum
                    sl.baseNum = data.data.baseNum
                    sl.stockWeight = data.data.stockWeight
                    sl.stockVolume = data.data.stockVolume
                    sl.stockMoney = data.data.stockMoney
                }
            });
            tableResult = table.render({
                elem: '#' + HxStorageStock.tableId,
                url: Feng.ctxPath + '/hxStorageStock/list?stockUnit=kc&queryMode=mx',
                where: queryData,
                page: true,
                limit: 120,
                limits:[60,120,240,360,480,600,2000,5000],
                height: "full-60",
                toolbar: '#toolbarfrom',
                cellMinWidth: 100,
                totalRow: true,//开启合计行
                cols: HxStorageStock.initColumn()
            });
        }

        if (stockUnit == "cg" && queryMode == "mx") {
            $.ajax({
                url: Feng.ctxPath + '/hxStorageStock/list2?stockUnit=cg&queryMode=mx',
                dataType: "json",   //返回格式为json
                data: queryData, // 将 queryData 作为请求体发送
                async: false,
                type: "post",   //请求方式
                success: function (data) {
                    sl.cgNum = data.data.cgNum
                    sl.baseNum = data.data.baseNum
                    sl.stockWeight = data.data.stockWeight
                    sl.stockVolume = data.data.stockVolume
                    sl.stockMoney = data.data.stockMoney
                }
            });
            tableResult = table.render({
                elem: '#' + HxStorageStock.tableId,
                url: Feng.ctxPath + '/hxStorageStock/list?stockUnit=cg&queryMode=mx',
                where: queryData,
                page: true,
                limit: 120,
                limits:[60,120,240,360,480,600,2000,5000],
                height: "full-60",
                toolbar: '#toolbarfrom',
                cellMinWidth: 100,
                totalRow: true,//开启合计行
                cols: HxStorageStock.initColumn1()
            });
        }
        if (stockUnit == "pf" && queryMode == "mx") {
            $.ajax({
                url: Feng.ctxPath + '/hxStorageStock/list2?stockUnit=pf&queryMode=mx',
                dataType: "json",   //返回格式为json
                data: queryData, // 将 queryData 作为请求体发送
                async: false,
                type: "post",   //请求方式
                success: function (data) {
                    sl.pfNum = data.data.pfNum
                    sl.baseNum = data.data.baseNum
                    sl.stockWeight = data.data.stockWeight
                    sl.stockVolume = data.data.stockVolume
                    sl.stockMoney = data.data.stockMoney
                }
            });
            tableResult = table.render({
                elem: '#' + HxStorageStock.tableId,
                url: Feng.ctxPath + '/hxStorageStock/list?stockUnit=pf&queryMode=mx',
                where: queryData,
                page: true,
                limit: 120,
                limits:[60,120,240,360,480,600,2000,5000],
                height: "full-60",
                toolbar: '#toolbarfrom',
                cellMinWidth: 100,
                totalRow: true,//开启合计行
                cols: HxStorageStock.initColumn2()
            });
        }
        if (stockUnit == "ps" && queryMode == "mx") {
            $.ajax({
                url: Feng.ctxPath + '/hxStorageStock/list2?stockUnit=ps&queryMode=mx',
                dataType: "json",   //返回格式为json
                data: queryData, // 将 queryData 作为请求体发送
                async: false,
                type: "post",   //请求方式
                success: function (data) {
                    sl.psNum = data.data.psNum
                    sl.baseNum = data.data.baseNum
                    sl.stockWeight = data.data.stockWeight
                    sl.stockVolume = data.data.stockVolume
                    sl.stockMoney = data.data.stockMoney
                }
            });
            tableResult = table.render({
                elem: '#' + HxStorageStock.tableId,
                url: Feng.ctxPath + '/hxStorageStock/list?stockUnit=ps&queryMode=mx',
                where: queryData,
                page: true,
                limit: 120,
                limits:[60,120,240,360,480,600,2000,5000],
                height: "full-60",
                toolbar: '#toolbarfrom',
                cellMinWidth: 100,
                totalRow: true,//开启合计行
                cols: HxStorageStock.initColumn3()
            });
        }
        if (stockUnit == "kc" && queryMode == "hz") {
            $.ajax({
                url: Feng.ctxPath + '/hxStorageStock/list2?stockUnit=kc&queryMode=hz',
                dataType: "json",   //返回格式为json
                data: queryData, // 将 queryData 作为请求体发送
                async: false,
                type: "post",   //请求方式
                success: function (data) {
                    sl.kcNum = data.data.kcNum
                    sl.baseNum = data.data.baseNum
                    sl.stockWeight = data.data.stockWeight
                    sl.stockVolume = data.data.stockVolume
                    sl.stockMoney = data.data.stockMoney
                }
            });

            tableResult = table.render({
                elem: '#' + HxStorageStock.tableId,
                url: Feng.ctxPath + '/hxStorageStock/list?stockUnit=kc&queryMode=hz',
                where: queryData,
                page: true,
                limit: 120,
                limits:[60,120,240,360,480,600,2000,5000],
                height: "full-60",
                toolbar: '#toolbarfrom',
                cellMinWidth: 100,
                totalRow: true,//开启合计行
                cols: HxStorageStock.initColumn4()
            });
        }

        if (stockUnit == "cg" && queryMode == "hz") {
            $.ajax({
                url: Feng.ctxPath + '/hxStorageStock/list2?stockUnit=cg&queryMode=hz',
                dataType: "json",   //返回格式为json
                data: queryData, // 将 queryData 作为请求体发送
                async: false,
                type: "post",   //请求方式
                success: function (data) {
                    sl.cgNum = data.data.cgNum
                    sl.baseNum = data.data.baseNum
                    sl.stockWeight = data.data.stockWeight
                    sl.stockVolume = data.data.stockVolume
                    sl.stockMoney = data.data.stockMoney
                }
            });
            tableResult = table.render({
                elem: '#' + HxStorageStock.tableId,
                url: Feng.ctxPath + '/hxStorageStock/list?stockUnit=cg&queryMode=hz',
                where: queryData,
                page: true,
                limit: 120,
                limits:[60,120,240,360,480,600,2000,5000],
                height: "full-60",
                toolbar: '#toolbarfrom',
                cellMinWidth: 100,
                totalRow: true,//开启合计行
                cols: HxStorageStock.initColumn5()
            });
        }


        if (stockUnit == "pf" && queryMode == "hz") {
            $.ajax({
                url: Feng.ctxPath + '/hxStorageStock/list2?stockUnit=pf&queryMode=hz',
                dataType: "json",   //返回格式为json
                data: queryData, // 将 queryData 作为请求体发送
                async: false,
                type: "post",   //请求方式
                success: function (data) {
                    sl.pfNum = data.data.pfNum
                    sl.baseNum = data.data.baseNum
                    sl.stockWeight = data.data.stockWeight
                    sl.stockVolume = data.data.stockVolume
                    sl.stockMoney = data.data.stockMoney
                }
            });
            tableResult = table.render({
                elem: '#' + HxStorageStock.tableId,
                url: Feng.ctxPath + '/hxStorageStock/list?stockUnit=pf&queryMode=hz',
                where: queryData,
                page: true,
                limit: 120,
                limits:[60,120,240,360,480,600,2000,5000],
                height: "full-60",
                toolbar: '#toolbarfrom',
                cellMinWidth: 100,
                totalRow: true,//开启合计行
                cols: HxStorageStock.initColumn6()
            });
        }

        if (stockUnit == "ps" && queryMode == "hz") {
            $.ajax({
                url: Feng.ctxPath + '/hxStorageStock/list2?stockUnit=ps&queryMode=hz',
                dataType: "json",   //返回格式为json
                data: queryData, // 将 queryData 作为请求体发送
                async: false,
                type: "post",   //请求方式
                success: function (data) {
                    sl.psNum = data.data.psNum
                    sl.baseNum = data.data.baseNum
                    sl.stockWeight = data.data.stockWeight
                    sl.stockVolume = data.data.stockVolume
                    sl.stockMoney = data.data.stockMoney
                }
            });
            tableResult = table.render({
                elem: '#' + HxStorageStock.tableId,
                url: Feng.ctxPath + '/hxStorageStock/list?stockUnit=ps&queryMode=hz',
                where: queryData,
                page: true,
                limit: 120,
                limits:[60,120,240,360,480,600,2000,5000],
                height: "full-60",
                toolbar: '#toolbarfrom',
                cellMinWidth: 100,
                totalRow: true,//开启合计行
                cols: HxStorageStock.initColumn7()
            });
        }
        if (stockUnit == "kc" && queryMode == "kw") {
            $.ajax({
                url: Feng.ctxPath + '/hxStorageStock/list2?stockUnit=kc&queryMode=kw',
                dataType: "json",   //返回格式为json
                data: queryData, // 将 queryData 作为请求体发送
                async: false,
                type: "post",   //请求方式
                success: function (data) {
                    sl.kcNum = data.data.kcNum
                    sl.baseNum = data.data.baseNum
                    sl.stockWeight = data.data.stockWeight
                    sl.stockVolume = data.data.stockVolume
                    sl.stockMoney = data.data.stockMoney
                }
            });

            tableResult = table.render({
                elem: '#' + HxStorageStock.tableId,
                url: Feng.ctxPath + '/hxStorageStock/list?stockUnit=kc&queryMode=kw',
                where: queryData,
                page: true,
                limit: 120,
                limits:[60,120,240,360,480,600,2000,5000],
                height: "full-60",
                toolbar: '#toolbarfrom',
                cellMinWidth: 100,
                totalRow: true,//开启合计行
                cols: HxStorageStock.initColumn8()
            });
        }

        if (stockUnit == "cg" && queryMode == "kw") {
            $.ajax({
                url: Feng.ctxPath + '/hxStorageStock/list2?stockUnit=cg&queryMode=kw',
                dataType: "json",   //返回格式为json
                data: queryData, // 将 queryData 作为请求体发送
                async: false,
                type: "post",   //请求方式
                success: function (data) {
                    sl.cgNum = data.data.cgNum
                    sl.baseNum = data.data.baseNum
                    sl.stockWeight = data.data.stockWeight
                    sl.stockVolume = data.data.stockVolume
                    sl.stockMoney = data.data.stockMoney
                }
            });
            tableResult = table.render({
                elem: '#' + HxStorageStock.tableId,
                url: Feng.ctxPath + '/hxStorageStock/list?stockUnit=cg&queryMode=kw',
                where: queryData,
                page: true,
                limit: 120,
                limits:[60,120,240,360,480,600,2000,5000],
                height: "full-60",
                toolbar: '#toolbarfrom',
                cellMinWidth: 100,
                totalRow: true,//开启合计行
                cols: HxStorageStock.initColumn9()
            });
        }


        if (stockUnit == "pf" && queryMode == "kw") {
            $.ajax({
                url: Feng.ctxPath + '/hxStorageStock/list2?stockUnit=pf&queryMode=kw',
                dataType: "json",   //返回格式为json
                data: queryData, // 将 queryData 作为请求体发送
                async: false,
                type: "post",   //请求方式
                success: function (data) {
                    sl.pfNum = data.data.pfNum
                    sl.baseNum = data.data.baseNum
                    sl.stockWeight = data.data.stockWeight
                    sl.stockVolume = data.data.stockVolume
                    sl.stockMoney = data.data.stockMoney
                }
            });
            tableResult = table.render({
                elem: '#' + HxStorageStock.tableId,
                url: Feng.ctxPath + '/hxStorageStock/list?stockUnit=pf&queryMode=kw',
                where: queryData,
                page: true,
                limit: 120,
                limits:[60,120,240,360,480,600,2000,5000],
                height: "full-60",
                toolbar: '#toolbarfrom',
                cellMinWidth: 100,
                totalRow: true,//开启合计行
                cols: HxStorageStock.initColumn10()
            });
        }

        if (stockUnit == "ps" && queryMode == "kw") {
            $.ajax({
                url: Feng.ctxPath + '/hxStorageStock/list2?stockUnit=ps&queryMode=kw',
                dataType: "json",   //返回格式为json
                data: queryData, // 将 queryData 作为请求体发送
                async: false,
                type: "post",   //请求方式
                success: function (data) {
                    sl.psNum = data.data.psNum
                    sl.baseNum = data.data.baseNum
                    sl.stockWeight = data.data.stockWeight
                    sl.stockVolume = data.data.stockVolume
                    sl.stockMoney = data.data.stockMoney
                }
            });
            tableResult = table.render({
                elem: '#' + HxStorageStock.tableId,
                url: Feng.ctxPath + '/hxStorageStock/list?stockUnit=ps&queryMode=kw',
                where: queryData,
                page: true,
                limit: 120,
                limits:[60,120,240,360,480,600,2000,5000],
                height: "full-60",
                toolbar: '#toolbarfrom',
                cellMinWidth: 100,
                totalRow: true,//开启合计行
                cols: HxStorageStock.initColumn11()
            });
        }
    };

    //查询库区
    form.on('select(warehouseId)', function () {
        $("#areaId").empty();
        $.ajax({
            url: Feng.ctxPath + '/basicArea/Querylist',
            dataType: "json",   //返回格式为json
            async: false,
            data: {"areaWarehouseId": $("#warehouseId").val()},    //参数值
            type: "post",   //请求方式
            success: function (data) {
                if (data.length > 0) {
                    var str = "<option value=''>选择库区</option>";
                    $.each(data, function (key, value) {
                        str += "<option value='" + value.id + "'>" + value.areaName + "</option>";
                    });
                    $("#areaId").html(str);
                }
            }
        });
        form.render('select');
    });
    /**
     * 弹出添加对话框
     */
    HxStorageStock.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/hxStorageStock/add';
    };
    // 添加按钮点击事件
    $('#add').click(function () {
        HxStorageStock.openAddDlg();
    });

    // 导入
    $('#btnExpimport').click(function () {
        layer.open({
            type: 2,
            title: '商品导入',
            area: ['1000px', '750px'],
            anim: 5,
            content: Feng.ctxPath + '/hxStorageStock/toimportExcel',
            end: function () {
                table.reload(HxStorageStock.tableId);
            }
        });
    });
    /**
     * 点击批量打印标签按钮
     */
    HxStorageStock.btnPrint = function () {
        var checkStatus = table.checkStatus('hxStorageStockTable');
        data = checkStatus.data;
        if (data.length > 0) {
            var ids = [];
            for (var i = 0; i < data.length; i++) {
                ids.push(data[i].id);
            }
            layer.open({
                type: 2,
                title: '批量打印标签码',
                area: ['900px', '550px'],
                anim: 5,
                content: Feng.ctxPath + '/hxStorageStock/tobtnqrCode?ids=' + ids + '&printQuantity=' + $("#printQuantity").val() + '&queryMode=' + $("#queryMode").val() + '&stockUnit=' + $("#stockUnit").val(),
                success: function (data) {
                }
            });
        } else {
            layer.alert('请至少选中一条数据');
        }
    };

    $('#btnPrint').click(function () {
        var stockUnit = $("#stockUnit").val();
        var queryMode = $("#queryMode").val();
        if (stockUnit == "ps" && queryMode == "mx") {
            HxStorageStock.btnPrint();
        } else {
            layer.alert('选择明细和配送单位才可以打印');
        }

    });
    //批量删除
    $('#btnRevoke').click(function () {
        var checkStatus = table.checkStatus('hxStorageStockTable');
        data = checkStatus.data;
        if (data.length > 0) {
            var ids = [];
            for (var i = 0; i < data.length; i++) {
                ids.push(data[i].id);
            }
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/hxStorageStock/btnRevoke?ids=" + ids, function (data) {
                    if (data.success) {
                        Feng.success("删除成功!");
                        table.reload(HxStorageStock.tableId);
                    } else {
                        Feng.error("删除失败!" + data.message + "!");
                    }
                }, function (data) {
                    Feng.error("删除失败!" + data.responseJSON.message + "!");
                });
                ajax.start();
            };
            Feng.confirm("是否删除选中明细?", operation);
        } else {
            layer.alert('请至少选中一条数据');
        }
    });
    /**
     * 导出excel按钮
     */
    HxStorageStock.exportExcel = function () {
        var checkRows = table.checkStatus(HxStorageStock.tableId);
        var originalColumns = HxStorageStock.initColumn()[0];
        var dataToExport = checkRows.data;
        // 移除数据
        dataToExport = dataToExport.map(function (row){
            delete row.id;
            delete row.center;
            return row;
        })
        // 移除表头
        originalColumns = originalColumns.filter(column => {
            return column.field !== 'id' && column.field !== 'center' && column.title !== '主键';
        })
        if (checkRows.data.length > 0) {
            layer.confirm('请选择导出格式：', {
                btn: ['xlsx', 'xls'], // 按钮
                skin: 'layui-layer-molv'
            }, function (index) {
                // exportToXlsx(dataToExport, originalColumns,'导出库存明细.xlsx');
                tableToExcel(dataToExport, originalColumns,'导出库存明细.xlsx');
                layer.close(index);
            }, function (index) {
                table.exportFile(tableResult.config.id, checkRows.data, 'xls');
                layer.close(index);
            });
        } else {
            var queryData = {};
            queryData['stockState'] = $("#stockState").val();
            queryData['setUnit'] = $("#setUnit").val();
            queryData['goodsBigtypeId'] = $("#goodsBigtypeId").val();
            queryData['goodsName'] = $("#goodsName").val();
            queryData['customerId'] = $("#customerId").val();
            queryData['warehouseId'] = $("#warehouseId").val();
            queryData['areaId'] = $("#areaId").val();
            queryData['positionNumber'] = $("#positionNumber").val();
            queryData['positionType'] = $("#positionType").val();
            queryData['trayNumber'] = $("#trayNumber").val();
            queryData['condition'] = $("#condition").val();

            var stockUnit = $("#stockUnit").val();
            var queryMode = $("#queryMode").val();
            $.ajax({
                type: "POST",
                url: Feng.ctxPath + "/hxStorageStock/listAll?stockUnit=" + stockUnit + "&queryMode=" + queryMode,
                dataType: 'json',
                data: queryData,
                beforeSend: function () {
                    //弹出加载中
                    index = layer.load(1, {
                        shade: [0.3, '#000'] //0.1透明度的白色背景
                    });
                },
                success: function (data) {
                    checkRows.data = data;
                    var allData = checkRows.data;
                    allData = allData.map(function (row){
                        delete row.id;
                        delete row.center;
                        return row;
                    })
                    // 弹出选择格式
                    layer.confirm('请选择导出格式：', {
                        btn: ['xlsx', 'xls'], // 按钮
                        skin: 'layui-layer-molv'
                    }, function (index) {
                        // exportToXlsx(allData, originalColumns,'导出库存明细.xlsx');
                        tableToExcel(allData, originalColumns,'导出库存明细.xlsx');
                        layer.close(index);
                    }, function (index) {
                        table.exportFile(tableResult.config.id, checkRows.data, 'xls');
                        layer.close(index);
                    });
                    layer.close(index);
                },
                error: function (data) {
                    // console.log(data)
                    layer.close(index);
                }
            });
        }
    };

    /**
     * 导出盘点表
     */
    HxStorageStock.exportPdExcel = function () {
        var checkRows = table.checkStatus(HxStorageStock.tableId);
        if (checkRows.data.length > 0) {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        } else {
            var stockUnit = $("#stockUnit").val();
            var queryMode = $("#queryMode").val();
            if (stockUnit === "kc" && queryMode === "kw") {
                window.location.href = Feng.ctxPath + "/hxStorageStock/exportToExcel?stockUnit=" + stockUnit + "&queryMode=" + queryMode +
                    '&stockState=' + $("#stockState").val() +
                    '&setUnit=' + $("#setUnit").val() +
                    '&goodsBigtypeId=' + $("#goodsBigtypeId").val() +
                    '&goodsName=' + $("#goodsName").val() +
                    '&customerId=' + $("#customerId").val() +
                    '&warehouseId=' + $("#warehouseId").val() +
                    '&areaId=' + $("#areaId").val() +
                    '&positionNumber=' + $("#positionNumber").val() +
                    '&positionType=' + $("#positionType").val() +
                    '&trayNumber=' + $("#trayNumber").val() +
                    '&condition=' + $('#condition').val();
            }else{
                layer.alert('选择库存和库存单位才可以导出');
            }
        };
    }

    // 导出excel
    $('#btnExp').click(function () {
        HxStorageStock.exportExcel();
    });

    // 导出盘点表
    $('#btnPdExp').click(function () {
        HxStorageStock.exportPdExcel();
    });

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    HxStorageStock.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/hxStorageStock/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    HxStorageStock.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/hxStorageStock/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(HxStorageStock.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    //点击选择商品档案
    $('#choiceGoods').click(function () {
        layer.open({
            type: 2,
            title: '商品档案',
            area: ['1000px', '500px'],
            content: Feng.ctxPath + '/hxBasicGoods/toChoiceGoods',
            end: function () {
                $("#goodsName").val(HxBasicGoodsInfoDlg.data.goodsName);//商品名称
            }
        });
    });

    function getCaption(obj) {//截取指定字符后的字符串
        var index = obj.lastIndexOf("|") + 1
        var res = obj.substring(index, obj.length)
        return res
    }

    //点击选择商品类别
    $('#goodsType').click(function () {
        var formName = encodeURIComponent("parent.DictInfoDlg.data.name");
        var formId = encodeURIComponent("parent.DictInfoDlg.data.dictId");
        var treeUrl = encodeURIComponent("/hxBasicGoodsCode/treeo");

        layer.open({
            type: 2,
            title: '所属分类',
            area: ['300px', '400px'],
            content: Feng.ctxPath + '/system/commonTree?formName=' + formName + "&formId=" + formId + "&treeUrl=" + treeUrl,
            end: function () {
                $("#goodsBigtypeId").val(DictInfoDlg.data.dictId);
                $("#goodsBigtype").val(getCaption(DictInfoDlg.data.name));
            }
        });
    });

    //选择发货客户
    $('#choiceSetUnit').click(function () {
        layer.open({
            type: 2,
            title: '发货客户',
            area: ['1000px', '500px'],
            content: Feng.ctxPath + '/basicPutgoods/tochoiceBasicPutgoods_hx',
            end: function () {
                $("#setUnit").val(BasicPutgoodsInfoDlg.data.putgoodsName);
            }
        });
    });
    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        HxStorageStock.search();
    });

    // 重置按钮点击事件
    $('#reset').click(function () {
        $("#goodsName").val("")
        $("#condition").val("")
        $("#goodsBigtypeId").val("")
        $("#goodsBigtype").val("")
        $("#setUnit").val("")
        $("#positionNumber").val("")
        $("#positionType").val("")
        $("#trayNumber").val("")
    });


    // 工具条点击事件
    table.on('tool(' + HxStorageStock.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            HxStorageStock.openEditDlg(data);
        } else if (layEvent === 'delete') {
            HxStorageStock.onDeleteItem(data);
        }
    });
});
